package com.ericsson.enap.modules.cdr.service.impl;

import com.ericsson.common.pojo.FTPInfoVO;
import com.ericsson.common.util.LogFileName;
import com.ericsson.common.util.LoggerUtils;
import com.ericsson.common.util.RedisCache;
import com.ericsson.enap.modules.cdr.entity.CdrCg;
import com.ericsson.enap.modules.cdr.mapper.CdrCgMapper;
import com.ericsson.enap.modules.cdr.service.CdrDownloadService;
import com.ericsson.enap.modules.cdr.thread.IMSCGCDRDownLoadThread;
import com.ericsson.enap.modules.cdr.thread.IMSOriginalCDRDownLoadThread;
import com.ericsson.enap.modules.cdr.thread.VGCCGCDRDownLoadSftpThread;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

@Lazy(true)
@Service("cdrDownloadService")
public class CdrDownloadServiceImpl implements CdrDownloadService {
    private Logger log = LoggerUtils.Logger(LogFileName.ImsCdrDownLoadJob);
    @Autowired
    private CdrCgMapper cdrCgMapper;
    @Autowired
    private RedisCache redisCache;
    @Override
    public List<CdrCg> getCgList(Integer biztype,Integer type,String collectorIp) {
        return cdrCgMapper.getCdrCgList(biztype,type,collectorIp);
    }

    @Override
    public void downloadCgCDRFile(Integer biztype, List<CdrCg> cdrCgs, String downloadDir, String cdrDownloadInterval) {
        for (CdrCg cdrCg : cdrCgs) {
            try {
                FTPInfoVO ftpInfo = new FTPInfoVO();
                ftpInfo.setHost(cdrCg.getIp4());
                ftpInfo.setPort(21);
                ftpInfo.setUserName(cdrCg.getUsername());
                ftpInfo.setPassword(cdrCg.getPassword());
                String path;
                String fileNameType="";
                if(cdrCg.getPath().contains("-")){
                    path=cdrCg.getPath().split("-")[0];
                    fileNameType=cdrCg.getPath().split("-")[1];
                }else{
                    path=cdrCg.getPath();
                }
                ftpInfo.setPath(path);
                ftpInfo.setCdrPathId(cdrCg.getPathId());
                ftpInfo.setNetElementID(cdrCg.getId());
                String lastFileModifyTime=(cdrCg.getLastFileModifyTime()==null) ?("0"):cdrCg.getLastFileModifyTime();
                String timeAndNum=(cdrCg.getTimeAndNum()==null) ?("0"):cdrCg.getTimeAndNum();
                String fileName=(cdrCg.getFileName()==null) ?(""):cdrCg.getFileName();
                /* todo */
                if(biztype==2) {//IMS采集类型
                    IMSCGCDRDownLoadThread task = new IMSCGCDRDownLoadThread(ftpInfo,
                        Long.parseLong(lastFileModifyTime), Long.parseLong(timeAndNum),fileName,downloadDir,cdrDownloadInterval,cdrCgMapper);
                    Thread t = new Thread(task);
                    t.start();
                }else if(biztype==4){
                    IMSOriginalCDRDownLoadThread task = new IMSOriginalCDRDownLoadThread(ftpInfo,downloadDir);
                    Thread t = new Thread(task);
                    t.start();
                }else if(biztype==12) {//5GC采集类型
                    VGCCGCDRDownLoadSftpThread task = new VGCCGCDRDownLoadSftpThread(fileNameType,ftpInfo,downloadDir,cdrDownloadInterval,redisCache);
                    Thread t = new Thread(task);
                    t.start();
                }
            }catch(Exception ex){
                log.error("downloadVgcCDRFile:cgId:"+cdrCg.getId()+"------biztype:"+biztype+ex.getMessage()+"path"+cdrCg.getPath());
            }
        }
    }
}
